package com.bojiu.lawsearch.lawsearch.controller;

import com.bojiu.lawsearch.lawsearch.model.Page;
import com.bojiu.lawsearch.lawsearch.model.Task;
import com.bojiu.lawsearch.lawsearch.service.TaskService;
import com.bojiu.lawsearch.lawsearch.util.AuthorizationHandle;
import com.bojiu.lawsearch.lawsearch.util.JsonResult;
import com.bojiu.lawsearch.lawsearch.util.ResultCode;
import com.google.gson.Gson;
import com.google.gson.JsonObject;
import io.swagger.annotations.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: afdiao
 * @Date：2020/4/12 21:48
 * @Description:工程师抢单接口
 */
@Api(description = "工程师操作任务接口")
@RestController
@RequestMapping("engineerTask")
@Slf4j
public class EngineerTaskController {

    @Autowired
    AuthorizationHandle authorizationHandle;

    @Autowired
    TaskService taskService;

    /**
     * 工程师查看任务列表
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @ApiOperation(value = "工程师获取任务列表", notes = "获取任务列表，返回工程师标签关联的所有的任务，只有状态为1的才能抢单")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Task.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("getTask/{pageNum}/{pageSize}")
    public JsonResult getTask(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        //判断用户权限
        JsonResult checkjsonResult = authorizationHandle.webHandler(request);
        if (!"200".equals(checkjsonResult.getCode())) {
            log.error("审核没有通过");
            return checkjsonResult;
        }
        //判断是否是工程师
        int userId = Integer.parseInt(String.valueOf(checkjsonResult.getData()));
        if (!authorizationHandle.isEngineer(userId)) {
            //不是工程师无法查看任务列表
            log.debug("不是工程师无法查看");
            return new JsonResult(ResultCode.NOAUTHENTION);
        }
        Page<Task> task = taskService.getTask(userId, pageSize, pageNum);
        return new JsonResult(ResultCode.SUCCESS, task);
    }

    /**
     * 工程师查看指派给自己的任务
     * @param pageNum
     * @param pageSize
     * @param request
     * @return
     */
    @ApiOperation(value = "工程师查看指派给自己的任务", notes = "工程师查看指派给自己的任务")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = Task.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("myTask/{pageNum}/{pageSize}")
    public JsonResult myTask(@ApiParam(name = "pageNum", value = "当前页码", required = true) @PathVariable("pageNum") int pageNum, @ApiParam(name = "pageSize", value = "每页个数", required = true) @PathVariable("pageSize") int pageSize, HttpServletRequest request) {
        //判断用户权限
        JsonResult checkjsonResult = authorizationHandle.webHandler(request);
        if (!"200".equals(checkjsonResult.getCode())) {
            log.error("审核没有通过");
            return checkjsonResult;
        }
        //判断是否是工程师
        int userId = Integer.parseInt(String.valueOf(checkjsonResult.getData()));
        if (!authorizationHandle.isEngineer(userId)) {
            //不是工程师无法查看任务列表
            log.debug("不是工程师无法查看");
            return new JsonResult(ResultCode.NOAUTHENTION);
        }
        Page<Task> task = taskService.getEngineerMyTask(userId, pageSize, pageNum);
        return new JsonResult(ResultCode.SUCCESS, task);
    }

    @ApiOperation(value = "工程师抢单", notes = "状态为1的任务工程师抢单")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @GetMapping("orderTask/{taskId}")
    public JsonResult orderTask(@ApiParam(name = "taskId", value = "任务id", required = true)@PathVariable("taskId") int taskId,HttpServletRequest request) {
        //判断用户权限
        JsonResult checkjsonResult = authorizationHandle.webHandler(request);
        if (!"200".equals(checkjsonResult.getCode())) {
            log.error("审核没有通过");
            return checkjsonResult;
        }
        //判断是否是工程师
        int userId = Integer.parseInt(String.valueOf(checkjsonResult.getData()));
        if (!authorizationHandle.isEngineer(userId)) {
            //不是工程师无法查看任务列表
            log.debug("不是工程师无法查看");
            return new JsonResult(ResultCode.NOAUTHENTION);
        }
        JsonResult result = taskService.orderTask(userId, taskId);
        return result;
    }

    @ApiOperation(value = "工程师回答问题", notes = "答题")
    @ApiResponses({
            @ApiResponse(code = 200, message = "成功", response = JsonResult.class),
            @ApiResponse(code = 600, message = "未检测到登录，跳转重新登录"),
            @ApiResponse(code = 500, message = "没有权限访问该接口")
    })
    @PostMapping("handleTask/{taskId}")
    public JsonResult handleTask(@ApiParam(name = "taskId", value = "任务id", required = true)@PathVariable("taskId") int taskId,@ApiParam(name = "text：工程师的回答", value = "json格式", required = true) @RequestBody String string, HttpServletRequest request) {
        //判断用户权限
        JsonResult checkjsonResult = authorizationHandle.webHandler(request);
        if (!"200".equals(checkjsonResult.getCode())) {
            log.error("审核没有通过");
            return checkjsonResult;
        }
        //判断是否是工程师
        int userId = Integer.parseInt(String.valueOf(checkjsonResult.getData()));
        if (!authorizationHandle.isEngineer(userId)) {
            //不是工程师无法查看任务列表
            log.debug("不是工程师无法查看");
            return new JsonResult(ResultCode.NOAUTHENTION);
        }
        String text = new Gson().fromJson(string, JsonObject.class).get("text").getAsString();
        JsonResult result = taskService.handleTask(taskId, userId, text);
        return result;
    }
    
}
